/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package processer;

import main.BotManager;
import main.Constants;
import utils.Debug;

/**
 *
 * @author Administrator
 */
public class TableChooser extends Processer {

    private int tableSelectedIndex;
    private boolean isWaitingMoney;

    public TableChooser(RoBot parent, GameData gameData, BotManager manager) {
        super(parent, gameData, manager);
        parent.request.updateMoney();
    }

    private void findTable() {
        tableSelectedIndex = -1;
        isWaitingMoney = false;
        int limitNumberPlayer = utils.Utils.nextInt(4);
        Debug.d("limit number :" + limitNumberPlayer);
        int i = 0;
        do {
            if (Debug.isTest) {
                tableSelectedIndex = 0;
            } else {
                tableSelectedIndex = utils.Utils.nextInt(20);
            }
            Debug.d("table select :" + tableSelectedIndex);
            if (gameData.numberPlayersInTable[tableSelectedIndex] > limitNumberPlayer) {
                tableSelectedIndex = -1;
                i++;
            } else {
                break;
            }
        } while (i < GameData.NUMBER_TABLES);

        Debug.d("table select :" + tableSelectedIndex);
        if (tableSelectedIndex == -1) {
            parent.request.exitChoooseTable();
        } else {
            parent.request.enterTable(tableSelectedIndex);
        }
    }

    @Override
    public void processMesage(String[] part) {
        super.processMesage(part);
        Debug.d("process table message:" + part[0]);
        if (part[0].equals("RO")) {
            gameData.setRoArrData(part);
            return;
        }
        if (part[0].equals("MO")) {
            gameData.setMoArrData(part);
            return;
        }
        if (part[0].equals("UMO")) {
            gameData.myMoney = Integer.parseInt(part[1]);
            if (gameData.myMoney <= 0) {
                parent.request.setMoney(100);
                isWaitingMoney = true;
            } else {
                findTable();
            }
            return;
        }

        if (part[0].equals("INFO")) {
            gameData.setInfoData(part);
            return;
        }
        if (part[0].equals("RE")) {
            gameData.setReArrData(part);
            gameData.setTableId(tableSelectedIndex);
            Debug.d("view id :" + gameData.getMyViewId());
            Debug.d("max player :" + gameData.getMaxPlayer());
            if (gameData.getMyViewId() < gameData.getMaxPlayer()) {
                parent.changeProcesser(RoBot.WAITING);
            } else {
                showViewMode();
            }
            return;
        }
        if (part[0].equals("GA")) {

            gameData.setGaData(part);
            parent.changeProcesser(RoBot.ROOM_CHOOSER);
            return;
        }
        if (part[0].equals("GM")) {
            gameData.setGameRule(Integer.parseInt(part[1]));
            return;
        }
        if (part[0].equals("CHESS")) {
            gameData.chessMapData = part;
            return;
        }
        if (part[0].equals("SM")) {
            gameData.setTableMoneyBet(part);
            return;
        }
        if (part[0].equals("USE")) {
            gameData.setNumberPlayerInTable(part);
            return;
        }
        if (part[0].equals("SE")) {
            gameData.setNumberPlayerInTable(part);
            return;
        }
        if (part[0].equals("PASS")) {
            gameData.setTablePass(part);
            return;
        }
        if (part[0].equals("RSE")) {
            parent.changeProcesser(RoBot.ROOM_CHOOSER);
            return;
        }
        if (part[0].equals("NRO")) {
            if (Debug.isTest) {
                parent.request.setMoney(gameData.myMoney * 5 + 100);
            } else {
                parent.request.setMoney(gameData.myMoney * 5 + 1000);
            }

            isWaitingMoney = true;
            return;
        }
        if (part[0].equals("SETMONEY")) {
            gameData.myMoney = Integer.parseInt(part[1]);
            if (isWaitingMoney) {
                parent.request.exitChoooseTable();
            }
            return;
        }
    }

    private void showViewMode() {
        switch (gameData.gameTypePlay) {
            case Constants.PHOM:
                parent.changeProcesser(RoBot.PLAYING_PHOM);
                break;
            case Constants.BA_CAY:
                parent.changeProcesser(RoBot.PLAYING_BA_CAY);
                break;
        }
    }

    @Override
    public void doLoop() {
    }
}
